/*
export function someGetter (state) {
}
*/

export const getInfoList = (state) => {
  return state.infoList
}

export const getCurInfo = (state) => {
  return state.curInfo
}

export const getMyList = (state) => {
  return state.myList
}

export const getCateList = (state) => {
  return state.cateList
}

// 三种类型

function getAccNextPriority (cert) {
  // 返回下一个需要审批的级别
  // 同时我们还可以看看有没有拒绝的，如果检查到拒绝的也可以返回
  const cate = cert.cate
  for (let i = 0; i < cate.restricts.length; i += 1) {
    const curRestrict = cate.restricts[i]
    // 下面看看属于这个限制的人数够了没有 如果没有 就是这个等级 如果够了 就返回等级+1
    const curRestrictNotAccPeople = cert.join_list.filter(o => o.restrict_id === curRestrict.id && o.status !== 'acc').length
    const isRej = cert.join_list.filter(o => o.restrict_id === curRestrict.id && o.status === 'rej').length !== 0
    if (isRej === true) {
      return {
        isRej: true
      }
    }
    if (curRestrictNotAccPeople !== 0) {
      return {
        restrict: curRestrict.id,
        priority: curRestrict.priority,
        isRej: false
      }
    }
  }
  console.log('never reach here')
}

function getRestrict (cate, user) {
  // 通过user的restrict_id找到cert对应的Priority
  return cate.restricts.filter(o => o.id === user.restrict_id)[0]
}
// 需要我审批的？必须上一个级别所有人都审批了 才能我这个级别审批 既然是我需要审批的 那么必须我在当前没有审批的级别之上
export function getMeAccept (state, getters, rootState, rootGetters) {
  const needMeAccept = []
  // 这里我们要分门别类将信息放好了
  const uid = rootGetters['session/getUser'].uid // 这里需要耦合session的user对象 应该怎么办呢
  state.relatedComp.forEach(cert => {
    const certState = getAccNextPriority(cert)
    if (certState.isRej !== true) {
      // 检查到现在还没有拒绝的 我们看当前用户所在的priority就可以确定是否需要我们同意了
      // 注意！全局不允许有相同的用户
      const findCurUser = cert.join_list.filter(o => o.uid === uid)[0]
      if (findCurUser.status === 'unn' && getRestrict(cert.cate, findCurUser).priority === certState.priority) {
        needMeAccept.push(cert)
      }
    }
  })
  return needMeAccept
}

export function getOtherAccept (state, getters, rootState, rootGetters) {
  const meAccept = getMeAccept(state, getters, rootState, rootGetters)
  const rejected = getRejected(state, getters, rootState, rootGetters)
  const waitingForOtherAccept = state.relatedComp.filter(o => meAccept.indexOf(o) === -1 && rejected.indexOf(o) === -1)
  return waitingForOtherAccept
}

export function getRejected (state, getters, rootState, rootGetters) {
  const rejected = []
  // 这里我们要分门别类将信息放好了
  state.relatedComp.forEach(cert => {
    const certState = getAccNextPriority(cert)
    if (certState.isRej === true) {
      rejected.push(cert)
    }
  })
  return rejected
}
// 三种类型

export function getNotifyNumber () {
  return getMeAccept().length
}
